package helipay.api.dao.impl;

import helipay.api.dao.MerchantDao;
import helipay.api.jdbc.StatementParameter;
import helipay.api.model.Merchant;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by yimi on 2017/6/2.
 */
@Repository
public class MerchantDaoImpl extends AbstractBaseDao<Merchant> implements MerchantDao {

    @Override
    public Merchant login(String customerNumber, String loginPassword) {

        StatementParameter parameter = new StatementParameter();
        parameter.setString(customerNumber);
        parameter.setString(loginPassword);

        return this.query("SELECT * FROM MERCHANT WHERE CUSTOMER_NUMBER = ? AND LOGIN_PASSWORD=?",Merchant.class,parameter);
    }

    @Override
    public Merchant getMerchant(String customerNumber) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT * FROM merchant WHERE CUSTOMER_NUMBER=?");
        StatementParameter parameter=new StatementParameter();
        parameter.setString(customerNumber);
        return query(sb.toString(),Merchant.class,parameter);
    }

    @Override
    public Integer changePassword(Long id,String newPassword) {
        StringBuffer getS = new StringBuffer();
        getS.append("update merchant SET LOGIN_PASSWORD=? ,UPDATED_TIME=now(),UPDATED_BY='admin' WHERE ID=?");
        StatementParameter parameter = new StatementParameter();
        parameter.setString(newPassword);
        parameter.setLong(id);

        return this.updateForRecord(getS.toString(), parameter);
    }

    @Override
    public List<Merchant> merchantList() {
        String sql="SELECT * FROM merchant ";

        return queryForList(sql,Merchant.class);
    }


}
